package com.leyou.item.web;

import com.leyou.common.dto.PageDTO;
import com.leyou.item.dto.BrandDTO;
import com.leyou.item.entity.Brand;
import com.leyou.item.service.BrandService;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
@RequestMapping("brand")
public class BrandController {

    private final BrandService brandService;

    public BrandController(BrandService brandService) {
        this.brandService = brandService;
    }

    //根据品牌id查询品牌
    @GetMapping("/{id}")
    public ResponseEntity<BrandDTO> queryBrandById(@PathVariable("id") Long id){
        return ResponseEntity.ok(new BrandDTO(brandService.getById(id)));
    }

    //根据品牌id集合查询品牌集合
    //不加@RequestParam会出错
    @GetMapping("/list")
    public ResponseEntity<List<BrandDTO>> queryBrandByIds(@RequestParam("ids") List ids){
        return ResponseEntity.ok(BrandDTO.convertEntityList(brandService.listByIds(ids)));
    }

    //根据分类id查询品牌
    @GetMapping("/of/category")
    public ResponseEntity<List<BrandDTO>> queryBrandByCategory(Long id){
        return ResponseEntity.ok(BrandDTO.convertEntityList(brandService.queryBrandByCategory(id)));
    }

    //分页查询
    @GetMapping("/page")
    public ResponseEntity<PageDTO<BrandDTO>>  queryBrandByPage(
            @RequestParam(value = "page", defaultValue = "1")Integer page,
            @RequestParam(value = "rows", defaultValue = "5")Integer rows,
            @RequestParam(value = "key", required = false)String key
    ){
        return ResponseEntity.ok(brandService.queryBrandByPage(page,rows,key));
    }

    //新增品牌
    @PostMapping
    public ResponseEntity saveBrand(BrandDTO brandDTO){
        brandService.saveBrand(brandDTO);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

    //更新品牌
    @PutMapping("/brand")
    public ResponseEntity updateBrand(BrandDTO brandDTO){
        brandService.updateBrand(brandDTO);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }

    //根据id删除
    @DeleteMapping("/{id}")
    public ResponseEntity<Void> deleteBrandById(@PathVariable("id") Long id){
        brandService.removeBrandById(id);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }

}